/**
 *
 *  1.支持chrome55+(需要支持es7的异步语法)，其它浏览器自行测试
 *
 *  2.该段代码不需要登录，但需要在 mobile.faxuan.net 域名下（能解决跨域的忽略这条）
 *
 *  3.支持多用户同时刷。
 *
 *  4.需要手动设置用户名(userName)和单位编码(domainCode)
 *  ※ domainCode 不是我取的,该系统的码农取的，但那个蛋疼的就是单位编码的意思
 *
 *  5.也支持在该登录用户下刷，需要改域名为 xf.faxuan.net ,注意跨域(能解决跨域的不用注意)
 *  或者可以这样
 *  let obj=new integral();
 *  obj.initialParameters();
 *  obj.excute2018();
 *  obj.excute2018Async();
 *
 *
 */

(function () {


    async function sleep(ms) {
        return new Promise((s,r)=>{
            ms<=0?s():setTimeout(s,ms);
        });
    }


    http={};

    http.post=function (url,obj) {
        return fetch(url,{
            method:"post"
        })

    };

    http.get=function (url,obj) {
        return fetch(url,{
            method:"get"
        })

    };




    let integral = function (userAccount,domainCode) {
        this.url="http://mobile.faxuan.net";
        this.userAccount=userAccount;
        this.domainCode=domainCode;
        this.time=1000;
    };



    integral.prototype.initialParameters=function(){
        let r1 = /userAccount":"([0-9]*)"/;
        let r2 = /domainCode":"([0-9]*)"/;
        let s2 = decodeURIComponent(document.cookie ? document.cookie : "");
        this.userAccount = r1.exec(s2) ? r1.exec(s2)[1] : "";
        this.domainCode = r2.exec(s2) ? r2.exec(s2)[1] : "";
    };



    /**
     * 刷答题分的
     * @returns {Promise.<void>}
     */
    integral.prototype.ePoint=async function() {
        return http.post(this.url+"/pss/service/postPoint?operateType=epoint" +
            "&userAccount=" + this.userAccount +
            "&domainCode=" + this.domainCode +
            "&"+"timestamp=" + new Date() +
            "&exerScore=60" +
            "&expointValue=20"
        );
    };


    /**
     * 刷10分钟的
     * @returns {Promise.<void>}
     */
    integral.prototype.sPoint=async function() {

        return http.get(this.url+"/pss/service/postPoint?operateType=spoint" +
            "&userAccount=" + this.userAccount +
            "&domainCode=" + this.domainCode +
            "&"+"timestamp=" + new Date() +
            "&stime=88"
        );
    };


    /**
     * 刷登录的
     * @returns {Promise.<void>}
     */
    integral.prototype.lPoint=async function() {
        return http.get(this.url+"/pss/service/postPoint?operateType=lpoint" +
            "&userAccount=" + this.userAccount +
            "&domainCode=" + this.domainCode +
            "&"+"timestamp=" + new Date()
        );
    };

    /**
     * 刷1分的
     * @returns {Promise.<void>}
     */
    integral.prototype.iPoint=async function() {
        return http.get(this.url+"/pss/service/postPoint?operateType=ipoint" +
            "&userAccount=" + this.userAccount +
            "&domainCode=" + this.domainCode +
            "&"+"timestamp=" + new Date()
        );
    };


    /**
     * 刷活动的
     * @returns {Promise.<void>}
     */
    integral.prototype.aPoint=async function() {
        return http.get(this.url+"/pss/service/postPoint?operateType=apoint" +
            "&userAccount=" + this.userAccount +
            "&domainCode=" + this.domainCode +
            "&"+"timestamp=" + new Date()
        );
    };


    /**
     * 异步执行2018年刷积分规则，得分通常是个随机数
     * @returns {Promise.<void>}
     */
    integral.prototype.excute2018Async=async function (interval=-1) {

        let oldTime=this.time;

        if(interval>=0){
            this.time=interval;
        }

        let count=20;
        let i=0;
        for(i=0;i<count;i++){
            await sleep(this.time);
            this.iPoint();
            console.log(this.userAccount+":1分-第"+(i+1)+"次");
        }

        await sleep(1000);

        //登录
        count=20;
        for(i=0;i<count;i++){
            await sleep(this.time);
            this.lPoint();
            console.log( this.userAccount+":登录-第"+(i+1)+"次\r\n");
        }

        await sleep(1000);

        //刷分
        count=30;
        for(i=0;i<count;i++){
            await sleep(this.time);
            this.ePoint();
            console.log(this.userAccount+":测验-第"+(i+1)+"次\r\n");
        }


        await sleep(1000);

        //时间
        count=20;
        for(i=0;i<count;i++){
            await sleep(this.time);
            this.sPoint();
            console.log(this.userAccount+"时间-第"+(i+1)+"次\r\n");
        }

        console.log(this.userAccount+"不带活动结束")

        this.time=oldTime;
    };


    /**
     * 同步刷
     * @param interval 设置请求之间的时间间隔
     * @returns {Promise.<void>}
     */
    integral.prototype.excute2018=async function (interval=-1) {

        let oldTime=this.time;

        if(interval>=0){
            this.time=interval;
        }


        let count=20;
        let i=0;
        for(i=0;i<count;i++){
            await sleep(this.time);
            await this.iPoint();
            console.log(this.userAccount+":1分-第"+(i+1)+"次");
        }

        await sleep(1000);

        //两次登录  +10
        count=20;
        for(i=0;i<count;i++){
            await sleep(this.time);
            await this.lPoint();
            console.log( this.userAccount+":登录-第"+(i+1)+"次\r\n");
        }

        await sleep(1000);

        //两次刷分  +60
        count=30;
        for(i=0;i<count;i++){
            await sleep(this.time);
            await this.ePoint();
            console.log(this.userAccount+":测验-第"+(i+1)+"次\r\n");
        }


        await sleep(1000);

        //三次时间  +40
        count=20;
        for(i=0;i<count;i++){
            await sleep(this.time);
            await this.sPoint();
            console.log(this.userAccount+"时间-第"+(i+1)+"次\r\n");
        }

        console.log(this.userAccount+"不带活动结束");

        this.time=oldTime;


    };


    //要刷你们自己的自己去刷



    //我的
    new integral("5329011660024","100008014001166").excute2018Async(0);


    //老爷子的正常刷
    new integral("5329260410018","100008014006041").excute2018(1);



})();

